package parkshop.pmcourse.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.utils.ConstantUtil;

/*
 *  "/customer/**" 路径下的拦截器
 *  
 *  do something user specific
 */
@Component
public class CustomerCtrlInterceptor implements HandlerInterceptor{
	
	public static final String path="/"+ConstantUtil.userTypeCustomer+"/**";

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
    	Object obj = session.getAttribute(ConstantUtil.nameOfCurUser);
        if (obj == null || !(obj instanceof User)) {
        		response.sendRedirect(request.getContextPath()); 
        	
        		return false;
        }
        User curUser=(User)obj;
    	if(!ConstantUtil.userTypeCustomer.equals(curUser.getType())){
    		
    		response.sendRedirect(request.getContextPath() + "/signin"); 
    		return false;
    	}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
